home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 103.9 KB | 3,358 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UCalc.cp
- // Copyright © 1985-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UCALC__
- #include "UCalc.h"
- #endif
-
- // Calc
-
- #ifndef __UCALCROWS__
- #include "UCalcRows.h"
- #endif
-
- #ifndef __UCALCCOLUMNS__
- #include "UCalcColumns.h"
- #endif
-
- #ifndef __UCALCCELLS__
- #include "UCalcCells.h"
- #endif
-
- // MacApp
-
- #ifndef __UDEBUG__
- #include "UDebug.h"
- #endif
-
- #ifndef __USECTIONMGR__
- #include "USectionMgr.h"
- #endif
-
- #ifndef __UVIEWSERVER__
- #include "UViewServer.h"
- #endif
-
- // Toolbox
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __STDLIB__
- #include <stdlib.h>
- #endif
-
- //----------------------------------------------------------------------------------------
- // Global variable definitions.
- //----------------------------------------------------------------------------------------
-
- ValueFormat gGeneralFormat; // SANE conversion formats
- ValueFormat gDecimalFormat;
- ValueFormat gNoDecimalFormat;
- ValueFormat gScientificFormat;
- ValueFormat gNoFormat;
- FormatRecord gDefaultFormat; // default cell format
-
- Pattern gColumnSeparatorPattern; // patterns for separator lines
- Pattern gRowSeparatorPattern;
-
-
- //----------------------------------------------------------------------------------------
- // ReadCellCoordinate:
- //----------------------------------------------------------------------------------------
- #pragma segment AReadFile
-
- void ReadCellCoordinate(TFile* aFile,
- RowNumber& r,
- ColumnNumber& c)
- {
- // Reads a cell coordinate from a file
-
- CPoint cellCoordinate;
- long count;
-
-
- count = sizeof(CPoint);
- FailOSErr(aFile->ReadData(&cellCoordinate, count));
- r = cellCoordinate.v;
- c = cellCoordinate.h;
- } // ReadCellCoordinate
-
-
-
- //========================================================================================
- // CLASS TCalcApplication
- //========================================================================================
- #undef Inherited
- #define Inherited TApplication
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcApplication, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcApplication destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCalcApplication::~TCalcApplication()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCalcApplication::ICalcApplication:
- //----------------------------------------------------------------------------------------
- #pragma segment AInit
-
- void TCalcApplication::ICalcApplication()
-
- {
- IApplication(kFileType, kSignature);
-
- gGeneralFormat.style = FLOATDECIMAL;
- gGeneralFormat.digits = kValuePrecision;
-
- gDecimalFormat.style = FIXEDDECIMAL;
- gDecimalFormat.digits = 2;
-
- gNoDecimalFormat.style = FIXEDDECIMAL;
- gNoDecimalFormat.digits = 0;
-
- gScientificFormat.style = FLOATDECIMAL;
- gScientificFormat.digits = 2;
-
- gNoFormat.style = FIXEDDECIMAL;
- gNoFormat.digits = -1;
-
- gDefaultFormat.fJustification = kDefaultJustification;
- gDefaultFormat.fStyle = kGeneral;
- gDefaultFormat.fDigits = 0;
- gDefaultFormat.fFontNumber = kCellFont;
- gDefaultFormat.fFontSize = kCellFontSize;
- gDefaultFormat.fFontStyle = 0;
-
- gColumnSeparatorPattern = qd.gray;
- gRowSeparatorPattern = qd.gray;
-
- // Suppress Linker dead-stripping of these classes
- MA_REGISTER_CLASS(TCalcWindow);
- MA_REGISTER_CLASS(TCellsView);
- MA_REGISTER_CLASS(TRowsView);
- MA_REGISTER_CLASS(TColumnsView);
- MA_REGISTER_CLASS(TEntryView);
- MA_REGISTER_CLASS(TCoordView);
- MA_REGISTER_CLASS(TPrimaryScroller);
- MA_REGISTER_CLASS(TRegionDesignator);
- MA_REGISTER_CLASS(TSecondaryScroller);
- } // TCalcApplication::ICalcApplication
-
- //----------------------------------------------------------------------------------------
- // TCalcApplication::AboutToLoseControl: Remove Edit menu buzzwords for incoming Desk
- // Accessory
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcApplication::AboutToLoseControl(Boolean convertClipboard)// override
- {
- SetEditCommandName(cCut, cStandardCut);
- SetEditCommandName(cCopy, cStandardCopy);
- SetEditCommandName(cClear, cStandardClear);
-
- Inherited::AboutToLoseControl(convertClipboard);
- } // TCalcApplication::AboutToLoseControl
-
- //----------------------------------------------------------------------------------------
- // TCalcApplication::DoMakeDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- TDocument* TCalcApplication::DoMakeDocument(CommandNumber /*itsCommandNumber*/, TFile* itsFile)
- {
- TCalcDocument* aCalcDocument = new TCalcDocument;
-
- CRect dimensions(1, 1, kMaxColumns, kMaxRows);
-
- aCalcDocument->ICalcDocument(itsFile, dimensions);
-
- return aCalcDocument;
- } // TCalcApplication::DoMakeDocument
-
- //----------------------------------------------------------------------------------------
- // TCalcApplication::MakeViewForAlienClipboard: Launch a view to represent the data found
- // in the Clipboard at application start-up time, or when returning from an excursion to
- // MultiFinder, or when returning from a Desk Accessory. This creates a clipboard for
- // 'CALC' scrap.
- //----------------------------------------------------------------------------------------
- #pragma segment AClipboard
-
- TView* TCalcApplication::MakeViewForAlienClipboard()// override
- {
- Handle calcScrap;
- long scrapOffset;
- TCalcDocument * clipDocument;
- TCellsView * clipView;
- ScrapInfoRecord scrapInfo;
- RowNumber r;
- ColumnNumber c;
- short i;
- short cellsRead;
- CPoint cellCoord;
- long offset;
- TRow * aRow;
- TColumn * aColumn;
- TCell * aCell;
- Boolean perm;
-
- // Before doing anything else, make sure the scrap contains my type
- if (GetScrap(NULL, kCalcScrapType, &offset) > 0)
- {
- clipDocument = NULL; // so failure handler knows to free it
- calcScrap = NULL;
-
- FailInfo fi;
-
- Try(fi)
- {
-
- calcScrap = NewPermHandle(0);
-
- perm = PermAllocation(TRUE);
- scrapOffset = GetScrap(calcScrap, kCalcScrapType, &offset);
- perm = PermAllocation(perm);
-
- // Only a negative result indicates an error--FailOSErr considers any non-zero result an error.
- if (scrapOffset < 0)
- FailOSErr((short)scrapOffset);
- scrapOffset = 0;
-
- ReadScrap(calcScrap, scrapOffset, (unsigned char*) & scrapInfo, sizeof(scrapInfo));
- FailMemError();
- clipDocument = new TCalcDocument;
- clipDocument->ICalcDocument(NULL, scrapInfo.selection);
- clipDocument->DoInitialState();
-
- // update the user selection to represent the actual selection in the scrap
- TRegionDesignator * userSelection = (TRegionDesignator *)clipDocument->GetUserSelection();
- if (userSelection)
- userSelection->SetDesignationRect(scrapInfo.selection);
-
- clipView = NULL;
- clipView = new TCellsView;
- clipView->ICellsView(clipDocument, TRUE, NULL);
- clipDocument->fCellsView = clipView;
-
- for (r = 1; r <= (clipDocument->fNoOfRows); r++)
- {
- aRow = new TRow;
- aRow->IRow(r);
- aRow->ReadFromScrap(calcScrap, scrapOffset);
- clipDocument->AddRow(aRow);
- }
-
- for (c = 1; c <= (clipDocument->fNoOfColumns); c++)
- {
- aColumn = new TColumn;
- aColumn->IColumn(c);
- aColumn->ReadFromScrap(calcScrap, scrapOffset);
- clipDocument->AddColumn(aColumn);
- }
-
- cellsRead = 0;
- for (i = 1; i <= (scrapInfo.noOfCells); i++)
- {
- ReadScrap(calcScrap, scrapOffset, (unsigned char*) & cellCoord, sizeof(cellCoord));
- aCell = clipDocument->GetCell(cellCoord.v, cellCoord.h);
- aCell->ReadFromScrap(calcScrap, scrapOffset);
- cellsRead = cellsRead + 1;
- }
-
- #if qDebug
- if (gIntenseDebugging)
- {
- fprintf(stderr, "MakeViewForAlienClipboard: cellsRead=%d scrapInfo.noOfCells=%d\n", cellsRead, scrapInfo.noOfCells);
- if (cellsRead != scrapInfo.noOfCells)
- ProgramBreak("MakeViewForAlienClipboard: Wrong number of cells");
- }
- #endif
-
- calcScrap = DisposeIfHandle(calcScrap);
-
- fi.Success();
- return clipView;
- }
- //end of Try
- else
- { //We Failed somewhere along the line
- calcScrap = DisposeIfHandle(calcScrap);
- clipDocument = (TCalcDocument *)(FreeIfObject(clipDocument));
-
- fi.ReSignal();
-
- return NULL; // this statement is never executed, but it does defeat
- // an xlC warning
- }
- }
- else
- return Inherited::MakeViewForAlienClipboard();
- } // TCalcApplication::MakeViewForAlienClipboard
-
-
- //========================================================================================
- // CLASS TCalcDocument
- //========================================================================================
- #undef Inherited
- #define Inherited TEditionDocument
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcDocument, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::TCalcDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- TCalcDocument::TCalcDocument()
- {
- fCellsView = NULL;
- fRowsView = NULL;
- fColumnsView = NULL;
- fEntryView = NULL;
- fCoordView = NULL;
-
- fEditRow = 0;
- fEditColumn = 0;
- fEditCell = NULL;
-
- fRows = NULL;
- fColumns = NULL;
- fCells = NULL;
- } // TCalcDocument::TCalcDocument
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::ICalcDocument:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcDocument::ICalcDocument(TFile* itsFile,
- const CRect& dimensions)
- {
- CRect aRect;
- TRowList * aRowList;
- TColumnList * aColumnList;
- TCellList * aCellList;
- TRegionDesignator * aRgnDesignator;
- RgnHandle aRgnHandle;
-
- this->IEditionDocument(itsFile, kCalcScrapType, kSignature);
-
- FailInfo fi;
- Try(fi)
- {
- fSavePrintInfo = TRUE; // the 'print info' record of the
- // fDocPrintHandler will be written out to
- // the data fork
-
- fDimensions = dimensions;
-
- fRowOffset = dimensions.top - 1;
- fColumnOffset = dimensions.left - 1;
- fNoOfRows = dimensions.bottom - dimensions.top + 1;
- fNoOfColumns = dimensions.right - dimensions.left + 1;
- fCalcMode = kCalcMode;
- fSelectionType = kNoSelection;
-
- fDependencies = new TStandardDependencySpace;
- fDependencies->IStandardDependencySpace();
-
- aRowList = new TRowList;
- aRowList->IRowList();
- fRows = aRowList;
-
- aColumnList = new TColumnList;
- aColumnList->IColumnList();
- fColumns = aColumnList;
-
- aCellList = new TCellList;
- aCellList->ICellList();
- fCells = aCellList;
-
- SetRect(aRect, 1, 1, 1, 1);
- fInUseBounds = aRect;
-
- // create the user selection
- aRgnHandle = MakeNewRgn();
- aRgnDesignator = new TRegionDesignator;
- aRgnDesignator->IRegionDesignator(aRgnHandle, TRUE);
- aRgnDesignator->SetDesignationRect(CRect(1, 1, 2, 2));
- SetUserSelection(aRgnDesignator);
- fi.Success();
- }
- else
- {
- this->Free();
- fi.ReSignal();
- }
- } // TCalcDocument::ICalcDocument
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoInitialState:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcDocument::DoInitialState() // override
- {
- fAllocatedCells = 0;
- fAllocatedRows = 0;
- fAllocatedColumns = 0;
- } // TCalcDocument::DoInitialState
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoMakeViews:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcDocument::DoMakeViews(Boolean forPrinting)
- {
- TCalcWindow * aCalcWindow;
- TEntryView * aTEntryView;
- TCellsView * aCellsView;
- TColumnsView * aColumnsView;
- TRowsView * aRowsView;
- TCoordView * aCoordView;
- TSecondaryScroller * aRowScroller;
- TSecondaryScroller * aColumnScroller;
- TPrimaryScroller * aCalcScroller;
- TCalcPrintHandler * aPrintHandler;
- VPoint aVPt;
-
- aCalcWindow = (TCalcWindow *)(gViewServer->NewTemplateWindow(kCalcWindowType, this));
- FailNIL(aCalcWindow); // We still must do FailNIL after NewTemplateWindow calls!!!
-
- aCellsView = (TCellsView *)(aCalcWindow->FindSubView('CELL'));
- aRowsView = (TRowsView *)(aCalcWindow->FindSubView('ROWS'));
- aColumnsView = (TColumnsView *)(aCalcWindow->FindSubView('COLS'));
- aTEntryView = (TEntryView *)(aCalcWindow->FindSubView('ENTV'));
- aCoordView = (TCoordView *)(aCalcWindow->FindSubView('CORD'));
-
-
- fCellsView = aCellsView;
- fRowsView = aRowsView;
- fColumnsView = aColumnsView;
- fEntryView = aTEntryView;
- fCoordView = aCoordView;
-
- // The TSectionBehavior is now added to the TCellsView in the 'View' resource (see Calc.r).
- // this->DoAddSectionBehavior(fCellsView); // add support for Ed Mgr shortcuts
-
- if (!forPrinting)
- {
- aTEntryView->fText = NULL;
- aTEntryView->fText = NewPermHandle(0);
- aTEntryView->StuffText(aTEntryView->fText);// Stuff the initial text in
- }
-
- // The entry view is the target. Its next handler is the cells view
- // TEntryView.DoPostCreate sets the fControlChars field so that Return, Enter etc.
- // are passed on to the cells view
-
- aCalcWindow->SetWindowTarget(aTEntryView);
- aTEntryView->fNextHandler = aCellsView;
-
- // set up the cells view scroller to scroll the rows and columns too
- aCalcScroller = (TPrimaryScroller *)(aCellsView->GetScroller(TRUE));
- SetVPt(aVPt, kCellWidth, kCellHeight);
- aCalcScroller->SetScrollParameters(aVPt, TRUE, TRUE);
-
- aColumnScroller = (TSecondaryScroller *)(aColumnsView->GetScroller(TRUE));
- SetVPt(aVPt, kCellWidth, 0);
- aColumnScroller->SetScrollParameters(aVPt, TRUE, TRUE);
-
- aRowScroller = (TSecondaryScroller *)(aRowsView->GetScroller(TRUE));
- SetVPt(aVPt, 0, kCellHeight);
- aRowScroller->SetScrollParameters(aVPt, TRUE, TRUE);
-
- aCalcScroller->AddSecondaryScroller(aRowScroller, kNotHDependent, kVDependent);
- aCalcScroller->AddSecondaryScroller(aColumnScroller, kHDependent, kNotVDependent);
-
- aPrintHandler = new TCalcPrintHandler;
- aPrintHandler->IStdPrintHandler(this, // its document
- aCellsView, // its view
- !kFixedSize, // horizontal page size is variable
- kFixedSize); // vertical page size is fixed
- aPrintHandler->fMinimalMargins = FALSE;
-
-
- // get existing document's column widths
- CCalcColumnIterator iter(this);
-
- for (TColumn * aColumn = iter.FirstColumn(); iter.More(); aColumn = iter.NextColumn())
-
- if (aColumn->fWidth != kCellWidth)
- {
- fCellsView->SetColWidth(aColumn->fNumber, 1, aColumn->fWidth);
- fColumnsView->SetColWidth(aColumn->fNumber, 1, aColumn->fWidth);
- }
-
- if (forPrinting) // Finder printing
- {
- aPrintHandler->RedoPageBreaks();
- }
- else
- {
- this->ShowReverted(); // display the views
-
- fEditRow = 1; // default cell to edit is A1
- fEditColumn = 1;
- this->SetEntry(fEditRow, fEditColumn);
- fSelectionType = kCellSelection;
- }
- } // TCalcDocument::DoMakeViews
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment AClose
-
- TCalcDocument::~TCalcDocument()
- {
- this->FreeData();
-
- fCells = (TCellList *)(FreeListIfObject(fCells));
- fRows = (TRowList *)(FreeListIfObject(fRows));
- fColumns = (TColumnList *)(FreeListIfObject(fColumns));
- fDependencies = (TStandardDependencySpace*) FreeIfObject(fDependencies);
- } // TCalcDocument::Free
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoToPublisher:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::DoToPublisher(TCell* theCell,
- TPublisher* aPublisher)
- {
- GridCell aCell;
-
-
- aCell.v = theCell->fRow;
- aCell.h = theCell->fColumn;
- if (PtInRgn(aCell, ((TRegionDesignator *)(aPublisher->fDesignator))->fDesignation))
- theCell->AddDependent(aPublisher);
- } // TCalcDocument::DoToPublisher
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::FreeData:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::FreeData()
- {
- fCells->FreeAll();
- fRows->FreeAll();
- fColumns->FreeAll();
-
- } // TCalcDocument::FreeData
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoNeedDiskSpace:
- //----------------------------------------------------------------------------------------
- #pragma segment AWriteFile
-
- void TCalcDocument::DoNeedDiskSpace(TFile* itsFile,
- long& dataForkBytes,
- long& rsrcForkBytes) // Override
- {
- // get Print record requirements
- Inherited::DoNeedDiskSpace(itsFile, dataForkBytes, rsrcForkBytes);
-
- dataForkBytes += sizeof(CalcDocDiskInfo) + sizeof(RowDiskInfo) * fAllocatedRows + sizeof(ColumnDiskInfo) * fAllocatedColumns;
-
- CCalcCellIterator iter(this);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- dataForkBytes += aCell->GetDiskSize(FALSE);
-
- } // TCalcDocument::DoNeedDiskSpace
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoRead:
- //----------------------------------------------------------------------------------------
- #pragma segment AReadFile
-
- void TCalcDocument::DoRead(TFile* aFile, Boolean forPrinting) // override
- {
- short noOfCells;
- short noOfRows;
- short noOfColumns;
- TCell * aCell;
- TRow * aRow;
- TColumn * aColumn;
- long count;
-
- MAVolatile(short, i);
-
- //MAVolatile(RowNumber, r);
- //MAVolatile(ColumnNumber, c);
-
- RowNumber r = 0; // cannot be volatile because it is passed by reference
- ColumnNumber c = 0; // cannot be volatile because it is passed by reference
-
- FailInfo fi;
-
- Try(fi)
- {
- Inherited::DoRead(aFile, forPrinting);
- // Get info about the document
- {
- CalcDocDiskInfo theDocInfo;
-
-
- count = sizeof(CalcDocDiskInfo);
- FailOSErr(aFile->ReadData(&theDocInfo, count));
-
- fDimensions = theDocInfo.dimensions;
- fCalcMode = theDocInfo.calcMode;
- noOfRows = theDocInfo.allocatedRows;
- noOfColumns = theDocInfo.allocatedColumns;
- fAllocatedCells = theDocInfo.allocatedCells;
- fSelectionType = kNoSelection;
- fEditRow = theDocInfo.editRow;
- fEditColumn = theDocInfo.editColumn;
-
- fNoOfRows = fDimensions.bottom - fDimensions.top + 1;
- fNoOfColumns = fDimensions.right - fDimensions.left + 1;
-
- // Save the number of cells to be read, then set fAllocatedCells to
- // zero. As each cell is read fAllocatedCells is incremented.
- // When we've finished, noOfCells must equal fAllocatedCells.
- noOfCells = fAllocatedCells;
- this->DoInitialState(); // clear allocation counts
- }
-
- r = 0;
- c = 0;
- i = 0; // Initialized so failure handler knows
- // where we died
-
- for (i = 1; i <= noOfRows; i++) // Get info about each row
- {
- count = sizeof(r);
- FailOSErr(aFile->ReadData(&r, count));
- aRow = this->GetRow(r);
- aRow->ReadFromDisk(aFile);
- }
-
- for (i = 1; i <= noOfColumns; i++) // Get info about each column
- {
- count = sizeof(c);
- FailOSErr(aFile->ReadData(&c, count));
- aColumn = this->GetColumn(c);
- aColumn->ReadFromDisk(aFile);
- }
-
- for (i = 1; i <= noOfCells; i++) // Read in the cells
- {
- ReadCellCoordinate(aFile, r, c);
- aCell = this->GetCell(r, c);
- aCell->ReadFromDisk(aFile);
- }
-
- #if qDebug
- if (gIntenseDebugging)
- {
- fprintf(stderr, "TCalcDocument.DoRead: noOfCells=%d", noOfCells);
- if (noOfCells != fAllocatedCells)
- {
- fprintf(stderr, "TCalcDocument.DoRead: Wrong number of cells. noOfCells=%d, fAllocatedCells=%d", noOfCells, fAllocatedCells);
- ProgramBreak("");
- }
- }
- #endif
-
- fi.Success();
- }
- else
- {
- // Oh Boy are we in trouble
- // Need to set back rows and columns to reflect the number read in so
- // the freedata routine (which will eventually be called by other
- // failure handlers on the stack) won't try to free unallocated objects
-
- if (c == 0)
- {
- // We died while reading the rows in
- fNoOfRows = r - 1;
- fNoOfColumns = 0;
- }
- else if (i == 0)
- {
- // Died in the columns
- fNoOfColumns = c - 1;
- }
-
- fi.ReSignal(); // tell everyone else we died too.
- }
- } // TCalcDocument::DoRead
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoWrite:
- //----------------------------------------------------------------------------------------
- #pragma segment AWriteFile
-
- void TCalcDocument::DoWrite(TFile* aFile, Boolean makingCopy) // Override
- {
- short cellsWritten;
- CRect r;
-
-
- Inherited::DoWrite(aFile, makingCopy);
-
- if (aFile != NULL)
- {
- // Write info about the document
- {
- CalcDocDiskInfo theDocInfo;
- long count;
-
- theDocInfo.dimensions = fDimensions;
- theDocInfo.calcMode = fCalcMode;
- theDocInfo.allocatedRows = fAllocatedRows;
- theDocInfo.allocatedColumns = fAllocatedColumns;
- theDocInfo.allocatedCells = fAllocatedCells;
- theDocInfo.selectionType = kNoSelection;
- theDocInfo.editRow = fEditRow;
- theDocInfo.editColumn = fEditColumn;
- count = sizeof(CalcDocDiskInfo);
- FailOSErr(aFile->WriteData(&theDocInfo, count));
- }
-
- {
- // Write info about each row
- CCalcRowIterator iter(this);
- for (TRow * aRow = iter.FirstRow(); iter.More(); aRow = iter.NextRow())
- aRow->WriteToDisk(aFile); // EachExistingRowDo(WriteRow);
- }
-
- {
- // Write info about each column
- CCalcColumnIterator iter(this);
- for (TColumn * aColumn = iter.FirstColumn(); iter.More(); aColumn = iter.NextColumn())
- // aColumn->fWidth = fColumnsView->GetColWidth(aCell.h);
- aColumn->WriteToDisk(aFile);
- // this was "EachExistingColumnDo(WriteColumn); "
- }
-
- cellsWritten = 0;
- {
- // Write out the cells
- CExistingCalcCellIterator iter(this);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- {
- aCell->WriteToDisk(aFile);
- cellsWritten = cellsWritten + 1;
- }
- // EachExistingCellDo(WriteCell);
- }
-
- #if qDebug
- if (cellsWritten != fAllocatedCells)
- ProgramBreak("DoWrite: Incorrect number of cells written");
- #endif
-
- }
- } // TCalcDocument::DoWrite
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::DoSetupMenus() // override
-
- {
- Inherited::DoSetupMenus();
-
- Enable(cRecalculate, TRUE);
- EnableCheck(cAutoCalc, TRUE, IsAutoCalc());
- EnableCheck(cManualCalc, TRUE, fCalcMode == kManual);
-
- if (fColumnIsSelected)
- {
- FormatRecord columnFormat = ColumnExists(fEditColumn) ?
- GetColumn(fEditColumn)->fFormat : gDefaultFormat;
-
- short justification = columnFormat.fJustification;
- EnableCheck(cSystemJustify, TRUE, justification == teFlushDefault);
- EnableCheck(cForceLeftJustify, TRUE, justification == teFlushLeft);
- EnableCheck(cRightJustify, TRUE, justification == teFlushRight);
- EnableCheck(cCenter, TRUE, justification == teCenter);
-
- TypeOfStyle theStyle = columnFormat.fStyle;
- EnableCheck(cGeneral, TRUE, theStyle == kGeneral);
- EnableCheck(cDecimal, TRUE, theStyle == kDecimalStyle);
- EnableCheck(cNoDecimal, TRUE, theStyle == kNoDecimal);
- EnableCheck(cScientific, TRUE, theStyle == kScientific);
- }
- else
- {
- Enable(cSystemJustify, FALSE);
- Enable(cForceLeftJustify, FALSE);
- Enable(cRightJustify, FALSE);
- Enable(cCenter, FALSE);
-
- Enable(cGeneral, FALSE);
- Enable(cDecimal, FALSE);
- Enable(cNoDecimal, FALSE);
- Enable(cScientific, FALSE);
- }
-
- } // TCalcDocument::DoSetupMenus
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcDocument::DoMenuCommand(CommandNumber aCommandNumber) // Override
- {
- switch (aCommandNumber)
- {
- case cRecalculate:
- {
- DoRecalculate(kForceAutomatic, kUpdateDependencies);
- break;
- }
- case cAutoCalc:
- {
- fCalcMode = kAutomatic;
- DoRecalculate(kForceAutomatic, kUpdateDependencies);
- break;
- }
- case cManualCalc:
- {
- fCalcMode = kManual;
- break;
- }
-
- case cGeneral:
- case cNoDecimal:
- case cDecimal:
- case cScientific:
- case cSystemJustify:
- case cForceLeftJustify:
- case cRightJustify:
- case cCenter:
- {
- TColumnFormatter* aColumnFormatter = new TColumnFormatter;
- aColumnFormatter->IFormatter(this, aCommandNumber);
- PostCommand(aColumnFormatter);
- break;
- }
-
- default:
- {
- Inherited::DoMenuCommand(aCommandNumber);
- break;
- }
- } // switch!
- } // TCalcDocument::DoMenuCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::AddCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::AddCell(TCell* theCell,
- RowNumber r,
- ColumnNumber c)
- {
- CRect aRect;
- CSectionIterator iter(this);
-
- theCell->fCalcDocument = this;
- theCell->fRow = r;
- theCell->fColumn = c;
-
- fCells->Insert(theCell);
- fAllocatedCells = fAllocatedCells + 1;
-
- SetRect(aRect, c, r, c, r);
- UnionRect(aRect, fInUseBounds, fInUseBounds);
-
- for (TSection * aSection = iter.FirstSection(); iter.More(); aSection = iter.NextSection())
- if (aSection->GetSectionType() == stPublisher)
- this->DoToPublisher(theCell, (TPublisher *)aSection);
- } // TCalcDocument::AddCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::CellExists:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- Boolean TCalcDocument::CellExists(RowNumber r, ColumnNumber c)
- {
- TCell * aCell;
-
-
- aCell = fCells->GetCell(r, c);
- return (aCell != NULL) && (!aCell->fDeleted);
- } // TCalcDocument::CellExists
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DeleteCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::DeleteCell(RowNumber r, ColumnNumber c)
- {
- TCell * theCell = GetExistingCell(r, c);
- if (theCell)
- {
- theCell->SetDeleteState(TRUE);
- fAllocatedCells = fAllocatedCells - 1;
- if (theCell == fEditCell)
- fEntryView->SetToString(gEmptyString);
- }
- } // TCalcDocument::DeleteCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::IsExistingCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- Boolean TCalcDocument::IsExistingCell(TCell* aCell)
-
- {
- // returns true if its an ALLOCATED cell
- return (!aCell->fDeleted);
- } // TCalcDocument::IsExistingCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::EditCell: Change the formula of the cell being edited to the CString in
- // the entry view
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::EditCell()
- {
- CStr255 theString;
-
-
- fEntryView->GetAsString(theString);
- if (fEditCell == NULL)
- fEditCell = GetCell(fEditRow, fEditColumn);
- fEditCell->SetToString(theString);
- } // TCalcDocument::EditCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::FreeCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::FreeCell(TCell* theCell)
- {
- #if qDebug
- if (fCells->GetCell(theCell->fRow, theCell->fColumn) != theCell)
- ProgramBreak("TCalcDocument.FreeCell: Cell table inconsistent");
- #endif
-
- fCells->Delete(theCell);
-
- theCell = (TCell *)(FreeIfObject(theCell));
- } // TCalcDocument::FreeCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::FreeDeletedCells:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::FreeDeletedCells()
- {
- CCalcCellIterator iter(this);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- if (aCell->fDeleted)
- this->FreeCell(aCell);
- } // TCalcDocument::FreeDeletedCells
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::GetCell: Return the cell object for the given coordinates. If a cell
- // object doesn't already exist, create one.
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- TCell* TCalcDocument::GetCell(RowNumber r, ColumnNumber c)
- {
- TCell * theCell;
-
-
- theCell = fCells->GetCell(r, c);
- if (theCell == NULL)
- {
- theCell = new TCell;
- theCell->ICell(this, r, c);
- this->AddCell(theCell, r, c);
- }
-
- return theCell;
- } // TCalcDocument::GetCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::GetExistingCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- TCell* TCalcDocument::GetExistingCell(RowNumber r, ColumnNumber c)
- {
- return fCells->GetCell(r, c);
- } // TCalcDocument::GetExistingCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::IsAutoCalc:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- Boolean TCalcDocument::IsAutoCalc()
- {
- return fCalcMode == kAutomatic;
- } // TCalcDocument::IsAutoCalc
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::SetEntry: Set the CString in TEntryView to the formula in the cell
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::SetEntry(RowNumber r, ColumnNumber c)
- {
- TCell * theCell = this->GetExistingCell(r, c);
-
- if (theCell && !theCell->fDeleted)
- {
- CStr255 theString;
- theCell->GetAsString(theString);
- fEntryView->SetToString(theString);
- }
- else
- fEntryView->SetToString(gEmptyString);
- } // TCalcDocument::SetEntry
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::UndeleteCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::UndeleteCell(RowNumber r, ColumnNumber c)
- {
- TCell * theCell;
-
- theCell = this->GetExistingCell(r, c);
- if (theCell != NULL)
- {
- theCell->SetDeleteState(FALSE);
- fAllocatedCells = fAllocatedCells + 1;
- }
- } // TCalcDocument::UndeleteCell
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::AddRow:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::AddRow(TRow* theRow)
- {
- fRows->Insert(theRow);
- fAllocatedRows = fAllocatedRows + 1;
-
- fInUseBounds.top = Min(fInUseBounds.top, theRow->fNumber);
- fInUseBounds.bottom = Max(fInUseBounds.bottom, theRow->fNumber);
- } // TCalcDocument::AddRow
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::GetRow:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- TRow* TCalcDocument::GetRow(RowNumber r)
- {
- TRow * theRow;
-
- theRow = fRows->GetRow(r);
- if (theRow == NULL)
- {
- theRow = new TRow();
- theRow->IRow(r);
- this->AddRow(theRow);
- }
-
- return theRow;
- } // TCalcDocument::GetRow
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::RowExists:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- Boolean TCalcDocument::RowExists(RowNumber r)
- {
- return fRows->GetRow(r) != NULL;
- } // TCalcDocument::RowExists
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::AddColumn:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::AddColumn(TColumn* theColumn)
- {
- fColumns->Insert(theColumn);
- fAllocatedColumns = fAllocatedColumns + 1;
-
- fInUseBounds.left = Min(fInUseBounds.left, theColumn->fNumber);
- fInUseBounds.right = Max(fInUseBounds.right, theColumn->fNumber);
- } // TCalcDocument::AddColumn
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::ColumnExists:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- Boolean TCalcDocument::ColumnExists(ColumnNumber c)
- {
- return (fColumns->GetColumn(c) != NULL);
- } // TCalcDocument::ColumnExists
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::GetColumn:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- TColumn* TCalcDocument::GetColumn(ColumnNumber c)
- {
- TColumn * theColumn;
-
- theColumn = fColumns->GetColumn(c);
-
- if (theColumn == NULL)
- {
- theColumn = new TColumn;
- theColumn->IColumn(c);
- this->AddColumn(theColumn);
- }
-
- return theColumn;
- } // TCalcDocument::GetColumn
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::CellInRange:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- Boolean TCalcDocument::CellInRange(short r,
- short c,
- CRect& range)
- {
- return (r >= range.top) && (r <= range.bottom) && (c >= range.left) && (c <= range.right);
- } // TCalcDocument::CellInRange
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::ConstrainToUsedCells: Given a range of cells, this returns the range of
- // cells that fall within the range of used cells. This is used to optimize performance so
- // that we don't try to operate on cells that we know have never been used (i.e.
- // allocated).
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::ConstrainToUsedCells(CRect& cellRange)
- {
- cellRange.top = Max(cellRange.top, fInUseBounds.top);
- cellRange.left = Max(cellRange.left, fInUseBounds.left);
- cellRange.bottom = Min(cellRange.bottom, fInUseBounds.bottom);
- cellRange.right = Min(cellRange.right, fInUseBounds.right);
- } // TCalcDocument::ConstrainToUsedCells
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoRecalculate:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcDocument::DoRecalculate(Boolean forceAutomatic,
- Boolean setDependents)
- {
- if (IsAutoCalc() || forceAutomatic)
- {
- CCalcCellIterator iter(this);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- aCell->Recalculate(forceAutomatic, setDependents);
- }
- else
- {
- CSelectedExistingCalcCellIterator iter(this);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- aCell->Recalculate(forceAutomatic, setDependents);
- }
- this->SetChangeCount(Max(fChangeCount + 1, 1));// enable Save - document may have changed
- } // TCalcDocument::DoRecalculate
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::SetUserSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment MAViewRes
-
- void TCalcDocument::SetUserSelection(TDesignator* newSelection)
- {
- Inherited::SetUserSelection(newSelection);
-
- if (fCellsView && fCellsView->fSelections)
- {
- TDesignator * userSelection = this->GetUserSelection();
- if ((userSelection) && userSelection->DescendsFrom(TRegionDesignator::GetClassDescStatic()))
- fCellsView->SetSelection(((TRegionDesignator *)(userSelection))->fDesignation, kDontExtend, kHighlight, kSelect);
- //fCellsView->ForceRedraw();
- }
- } // TCalcDocument::SetUserSelection
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::UserSelectionChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcDocument::UserSelectionChanged(TView* changedView)// override
- {
- // let the document's selection designator know that the designation has changed
- TDesignator * userSelection = this->GetUserSelection();
- if ((userSelection != NULL) && userSelection->DescendsFrom(TRegionDesignator::GetClassDescStatic()))
- CopyRgn(fCellsView->fSelections, ((TRegionDesignator *)(userSelection))->fDesignation);
-
- Inherited::UserSelectionChanged(changedView);
- } // TCalcDocument::UserSelectionChanged
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoWriteData:
- //----------------------------------------------------------------------------------------
- #pragma segment ANonRes
-
- void TCalcDocument::DoWriteData(const OSType aScrapType,
- TDesignator* aDesignator,
- TStream* aStream)// override
- {
- if (aScrapType == 'TEXT')
- {
- Handle textHandle = fCellsView->DesignatorAsTEXT(aDesignator);
- SignedByte savedState = LockHandleHigh(textHandle);
-
- aStream->WriteBytes((*textHandle), GetHandleSize(textHandle));
-
- HSetState(textHandle, savedState);
- DisposeHandle(textHandle);
- }
- else if (aScrapType == fScrapType)
- this->DoWritePrivateTypes(aDesignator, aStream);
- } // TCalcDocument::DoWriteData
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoWritePrivateTypes:
- //----------------------------------------------------------------------------------------
- #pragma segment ANonRes
-
- void TCalcDocument::DoWritePrivateTypes(TDesignator* aDesignator,
- TStream* aStream)
- {
- CRect selection;
- short i;
- RowDiskInfo rowInfo;
- ColumnDiskInfo columnInfo;
-
- if (aDesignator->DescendsFrom(TRegionDesignator::GetClassDescStatic()))
- {
- // Write out extent.
- selection = GetRgnBounds(((TRegionDesignator *)(aDesignator))->fDesignation);
- aStream->WriteBytes(&selection, sizeof(selection));
-
- // Write out row info.
- for (i = selection.top; i <= selection.bottom; i++)
- {
- rowInfo.number = GetRow(i)->fNumber;// Need a GetRowDiskInfo().
- aStream->WriteBytes(&rowInfo, sizeof(rowInfo));
- }
-
- // Write out column info.
- for (i = selection.left; i <= selection.right; i++)
- {
- TColumn * aColumn; //temporary variable scoped to the for loop only!!!
-
- aColumn = this->GetColumn(i);
- columnInfo.number = aColumn->fNumber;
- columnInfo.format = aColumn->fFormat;
- columnInfo.width = aColumn->fWidth;
- aStream->WriteBytes(&columnInfo, sizeof(columnInfo));
- }
-
- // Write out cell info.
- CCellIterator iter(fCellsView, selection[topLeft], selection[botRight], kIterateForward, kIterateForward, CCellIterator::kIterateRowMajor);
- for (GridCell aGridCell = iter.FirstCell(); iter.More(); aGridCell = iter.NextCell())
- {
- CellDiskInfo cellInfo;
- TCell * aCell;
-
- // write out cell info
- aCell = this->GetCell(aGridCell.v, aGridCell.h);
- cellInfo.kind = aCell->fKind;
- cellInfo.error = aCell->fError;
- cellInfo.value = aCell->fValue;
- cellInfo.noOfReferences = 0;
- cellInfo.formula = aCell->fFormula;
- aStream->WriteBytes(&cellInfo, sizeof(cellInfo));
- }
- }
- else if (qDebug)
- ProgramBreak("I don't recognize that designator…sigh!");
- } // TCalcDocument::DoWritePrivateTypes
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoReadData:
- //----------------------------------------------------------------------------------------
- #pragma segment ANonRes
-
- void TCalcDocument::DoReadData(const OSType aScrapType,
- TDesignator* aDesignator,
- TStream* aStream,
- long length) // override
- {
- if (aScrapType == 'TEXT')
- this->DoReadTEXT(aDesignator, aStream, length);
- else if (aScrapType == fScrapType)
- this->DoReadPrivateTypes(aDesignator, aStream);
- } // TCalcDocument::DoReadData
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoReadPrivateTypes:
- //----------------------------------------------------------------------------------------
- #pragma segment ANonRes
-
- void TCalcDocument::DoReadPrivateTypes(TDesignator* aDesignator,
- TStream* aStream)
-
- {
- CRect subBounds; // bounds of the subscriber
- CRect pubBounds; // bounds of the publisher
- short i;
- RowDiskInfo rowInfo;
- ColumnDiskInfo columnInfo;
- CPoint difference;
-
- // read in the publisher bounds and figure out the difference
- subBounds = GetRgnBounds(((TRegionDesignator *)(aDesignator))->fDesignation);
- aStream->ReadBytes(&pubBounds, sizeof(pubBounds));
- difference.h = subBounds.left - pubBounds.left;
- difference.v = subBounds.top - pubBounds.top;
-
- // Read in row info.
- for (i = pubBounds.top; i <= pubBounds.bottom; i++)
- aStream->ReadBytes(&rowInfo, sizeof(rowInfo));
-
- // Read in the col info and set it.
- for (i = pubBounds.left; i <= pubBounds.right; i++)
- {
- aStream->ReadBytes(&columnInfo, sizeof(columnInfo));
- if ((i + difference.h >= subBounds.left) && (i + difference.h <= subBounds.right) && (!ColumnExists(i)))
- {
- TColumn * aColumn; //local variable to do WITH scoped to this "if" only
-
- aColumn = this->GetColumn(i);
- aColumn->fNumber = columnInfo.number;
- aColumn->fFormat = columnInfo.format;
- aColumn->fWidth = columnInfo.width;
- }
- }
-
- // Read in the cell info.
- CCellIterator iter(fCellsView, pubBounds[topLeft], pubBounds[botRight], kIterateForward, kIterateForward, CCellIterator::kIterateRowMajor);
- for (GridCell aGridCell = iter.FirstCell(); iter.More(); aGridCell = iter.NextCell())
- {
- TCell * aCell;
- CellDiskInfo cellInfo;
-
- // map a published cell into subscriber range
- aGridCell.v += difference.v;
- aGridCell.h += difference.h;
- aCell = this->GetCell(aGridCell.v, aGridCell.h); // retrieve or create a TCell
- aStream->ReadBytes(&cellInfo, sizeof(cellInfo));
- if ((aGridCell.h >= subBounds.left) && (aGridCell.h <= subBounds.right) && (aGridCell.v >= subBounds.top) && (aGridCell.v <= subBounds.bottom))
- if ((aCell->fKind != cellInfo.kind) || (aCell->fError != cellInfo.error) || (aCell->fValue != cellInfo.value) || (aCell->fFormula != cellInfo.formula))
- {
- aCell->fFormula = cellInfo.formula;
- aCell->Recalculate(TRUE, TRUE);
- aCell->invalidate();
- }
- } // we're taken care of.
- } // TCalcDocument::DoReadPrivateTypes
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoReadTEXT:
- //----------------------------------------------------------------------------------------
- #pragma segment ANonRes
-
- void TCalcDocument::DoReadTEXT(TDesignator* aDesignator,
- TStream* aStream,
- long length)
-
- {
- CRect subBounds; // bounds of the subscriber
- CRect pubBounds; // bounds of the publisher
- Handle theText;
-
-
- theText = NewPermHandle(length);
- LockHandleHigh(theText);
- aStream->ReadBytes((*theText), length);
- subBounds = GetRgnBounds(((TRegionDesignator *)(aDesignator))->fDesignation);
- {
- CCellIterator iter(fCellsView, subBounds[topLeft], subBounds[botRight], kIterateForward, kIterateForward, CCellIterator::kIterateRowMajor);
- CParseSubscribedText theParser(aDesignator, aStream, length, subBounds, pubBounds);
- for (GridCell aGridCell = iter.FirstCell(); iter.More(); aGridCell = iter.NextCell())
- {
- CStr255 aStr;
- TCell * aCell;
-
- aStr = theParser.ParseSubscribedText(theText, aGridCell);
- aCell = this->GetCell(aGridCell.v, aGridCell.h);// retrieve or create a TCell
- aCell->SetToString(aStr);
- }
- }
- DisposeHandle(theText);
- } // TCalcDocument::DoReadTEXT
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::AddPublisherDependency:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcDocument::AddPublisherDependency(TSection* aSection)
- {
- if (fCellsView && (aSection->GetSectionType() == stPublisher))
- {
- // make each publisher a dependent of each cell in the designation
-
- CCellInRegionIterator iter(fCellsView, ((TRegionDesignator *)(aSection->fDesignator))->fDesignation);
- for (GridCell aGridCell = iter.FirstCell(); iter.More(); aGridCell = iter.NextCell())
- {
- if (this->CellExists(aGridCell.v, aGridCell.h))
- {
- TCell * aCell = this->GetCell(aGridCell.v, aGridCell.h);
- aCell->AddDependent(aSection);
- }
- }
- }
- } // TCalcDocument::AddPublisherDependency
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::AddSectionAndBorder:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcDocument::AddSectionAndBorder(TSection* aSection)
- {
- Inherited::AddSectionAndBorder(aSection);
-
- this->AddPublisherDependency(aSection);
- } // TCalcDocument::AddSectionAndBorder
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoPostMakeViews:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcDocument::DoPostMakeViews(Boolean forPrinting)
- {
- if (!forPrinting)
- {
- CSectionIterator iter(this);
-
- for (TSection * aSection = iter.FirstSection(); iter.More(); aSection = iter.NextSection())
- this->AddPublisherDependency(aSection);
- }
-
- Inherited::DoPostMakeViews(forPrinting);
- } // TCalcDocument::DoPostMakeViews
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoAddBorder:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcDocument::DoAddBorder(TSection* aSection)
- {
- // adding a section, give it a section adorner
- if (fCellsView)
- {
- if (aSection->GetSectionType() == stSubscriber)
- fCellsView->CreateSubscriberAdorner((TSubscriber *)aSection);
- else
- fCellsView->CreatePublisherAdorner((TPublisher *)aSection);
-
- // invalidate the borders of publishers/subscribers, if we aren't showing all borders
- if (!fShowSectionBorders)
- {
- CAdornerIterator iter(fCellsView);
-
- for (TAdorner * anAdorner = iter.FirstAdorner(); iter.More(); anAdorner = iter.NextAdorner())
- if ((anAdorner->fIdentifier == kPublisherAdornerID)
- || (anAdorner->fIdentifier == kSubscriberAdornerID))
- anAdorner->InvalidateAdorner(fCellsView);
- }
- }
- } // TCalcDocument::DoAddBorder
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::DoDeleteBorder:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcDocument::DoDeleteBorder(TSection* aSection)
- {
- // adding a section, give it a section adorner
- CAdornerIterator iter(fCellsView);
-
- for (TAdorner * anAdorner = iter.FirstAdorner(); iter.More(); anAdorner = iter.NextAdorner())
- if ((anAdorner->fIdentifier == kPublisherAdornerID)
- || (anAdorner->fIdentifier == kSubscriberAdornerID))
- if (((TSectionAdorner *)anAdorner)->fSection == aSection)
- {
- fCellsView->DeleteAdorner(anAdorner, kInvalidate);
- break;
- }
-
- // invalidate the borders of publishers/subscribers, if we aren't showing all borders
- if (!fShowSectionBorders)
- {
- CAdornerIterator iter(fCellsView);
-
- for (TAdorner * anAdorner = iter.FirstAdorner(); iter.More(); anAdorner = iter.NextAdorner())
- if ((anAdorner->fIdentifier == kPublisherAdornerID)
- || (anAdorner->fIdentifier == kSubscriberAdornerID))
- anAdorner->InvalidateAdorner(fCellsView);
- }
- } // TCalcDocument::DoDeleteBorder
-
-
- //----------------------------------------------------------------------------------------
- // TCalcDocument::RevealSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcDocument::RevealSelection(TDesignator* aSelection)// override
- {
- CRect qdExtent;
- VRect viewRect;
-
- Inherited::RevealSelection(aSelection); // make window front-most
-
- if (aSelection->DescendsFrom(TRegionDesignator::GetClassDescStatic()))
- {
- fCellsView->SetSelection(((TRegionDesignator *)(aSelection))->fDesignation, kDontExtend, kHighlight, kSelect);
-
- fCellsView->ScrollSelectionIntoView(kRedraw);
- }
- else if (qDebug)
- ProgramBreak("handed an unknown designator");
- } // TCalcDocument::RevealSelection
-
-
- //========================================================================================
- // CLASS TCalcWindow
- //========================================================================================
- #undef Inherited
- #define Inherited TWindow
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcWindow, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcWindow destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCalcWindow::~TCalcWindow()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCalcWindow::DoPostCreate:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCalcWindow::DoPostCreate(TDocument*/* itsDocument */ ) // Override
- {
- CPoint minSize;
-
- minSize.h = 284;
- minSize.v = 126;
- this->SetResizeLimits(minSize, fResizeLimits[botRight]);
- } // TCalcWindow::DoPostCreate
-
-
- //----------------------------------------------------------------------------------------
- // TCalcWindow::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcWindow::Draw(const VRect& area) // override
- {
- // draw the extra borders for the rows and columns views
- PenNormal();
-
- // rows
- MoveTo(0, 35);
- Line(kRowTitleWidth - 1, 0);
-
- MoveTo(0, 35 + kCellHeight - 1);
- Line(kRowTitleWidth - 1, 0);
-
- // columns
- MoveTo(kRowTitleWidth - 1, 35);
- Line(0, kCellHeight - 1);
-
- Inherited::Draw(area);
- } // TCalcWindow::Draw
-
-
-
- //========================================================================================
- // CLASS TCellsView
- //========================================================================================
- #undef Inherited
- #define Inherited TTextGridView
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCellsView, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCellsView destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCellsView::~TCellsView()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCellsView::CreatePublisherAdorner:
- //----------------------------------------------------------------------------------------
- #pragma segment GNonRes2
-
- void TCellsView::CreatePublisherAdorner(TPublisher* aPublisher)
- {
- TPublisherAdorner * aPublisherAdorner;
- RgnHandle itsBorderRegion;
-
- itsBorderRegion = MakeNewRgn();
- this->ComputeBorderRgn(itsBorderRegion, ((TRegionDesignator *)(aPublisher->fDesignator))->fDesignation);
-
- aPublisherAdorner = new TPublisherAdorner;
- aPublisherAdorner->IPublisherAdorner(kPublisherAdornerID, aPublisher, itsBorderRegion);
-
- this->AddAdorner(aPublisherAdorner, kAdornAfter, fCalcDocument->IsBorderShown(aPublisher));
- } // TCellsView::CreatePublisherAdorner
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::CreateSubscriberAdorner:
- //----------------------------------------------------------------------------------------
- #pragma segment GNonRes2
-
- void TCellsView::CreateSubscriberAdorner(TSubscriber* aSubscriber)
- {
- TSubscriberAdorner * aSubscriberAdorner;
- RgnHandle itsBorderRegion;
-
- itsBorderRegion = MakeNewRgn();
- this->ComputeBorderRgn(itsBorderRegion, ((TRegionDesignator *)(aSubscriber->fDesignator))->fDesignation);
-
- aSubscriberAdorner = new TSubscriberAdorner;
- aSubscriberAdorner->ISubscriberAdorner(kSubscriberAdornerID, aSubscriber, itsBorderRegion);
-
- this->AddAdorner(aSubscriberAdorner, kAdornAfter, fCalcDocument->IsBorderShown(aSubscriber));
- } // TCellsView::CreateSubscriberAdorner
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ComputeBorderRgn:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::ComputeBorderRgn(RgnHandle itsBorderRegion,
- RgnHandle itsDesignation)
-
- {
- CTemporaryRegion tempRgn;
-
- if (this->Focus())
- this->CellsToPixels(itsDesignation, tempRgn);
-
- RectRgn(itsBorderRegion, CRect((*tempRgn)->rgnBBox) + CRect(-5, -5, 4, 4));
- RectRgn(tempRgn, CRect((*tempRgn)->rgnBBox) + CRect(-2, -2, 1, 1));
-
- DiffRgn(itsBorderRegion, tempRgn, itsBorderRegion);
- } // TCellsView::ComputeBorderRgn
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ICellsView:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCellsView::ICellsView(TCalcDocument* itsDocument,
- Boolean /*forClipboard*/,
- TView* itsParent)
- {
- this->ITextGridView(itsDocument, itsParent, gZeroVPt, gZeroVPt, sizeVariable, sizeVariable, itsDocument->fNoOfRows, itsDocument->fNoOfColumns, 0, kCellWidth, kAdorn, kAdorn, 0, 0, FALSE, gSystemStyle);
-
- fCalcDocument = itsDocument;
- fLastOptionKey = FALSE; // Used for DoSetCursor to the grabber hand
-
- fIdleFreq = Max(GetCaretTime() / 2, (long)1);// So we can trackCursor for the GrabberHand.
- // With MF 7.0 we could setup a VBL that monitered the modifiers and called Wakeup
- // with our PID. Even better would be a call that set the event manager so that an
- // event could be returned on modifier key changes… oh, well.
-
- fHelpID = kCellsViewHelp;
- fHelpIndex = 1;
- } // TCellsView::ICellsView
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoPostCreate:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCellsView::DoPostCreate(TDocument* itsDocument)
- {
- fCalcDocument = (TCalcDocument *)(itsDocument);
- fHelpID = kCellsViewHelp;
- fHelpIndex = 1;
- } // TCellsView::DoPostCreate
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoMouseCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCellsView::DoMouseCommand(VPoint& theMouse,
- TToolboxEvent* event,
- CPoint /*hysteresis*/) // override
- {
- TCalcSelectCommand * aCellSelector;
- TColumnSizer * aColumnSizer;
- GridCell aCell(-1, -1);
- GridViewPart whichPart;
- short aRow = -1; // This value should never be used
- short aCol = -1; // This value should never be used
- TGrabberTracker * aGrabber;
-
- whichPart = this->IdentifyPoint(theMouse, aCell); //since almost ß
- aCol = aCell.h;
- aRow = aCell.v;
-
- if (event->IsOptionKeyPressed())
- {
- aGrabber = new TGrabberTracker;
- aGrabber->IGrabberTracker(cNoCommand, fCalcDocument, this, GetScroller(FALSE), theMouse);
- this->PostCommand(aGrabber);
- }
- else
- {
- switch (whichPart)
- {
- case inCell:
- {
- aCellSelector = new TCalcSelectCommand;
- aCellSelector->ICalcSelectCommand(fCalcDocument, this, theMouse, event->IsShiftKeyPressed(), event->IsCommandKeyPressed());
- this->PostCommand(aCellSelector);
- fCalcDocument->fSelectionType = kCellSelection;
- break;
- }
-
- case inColumn:
- case inVertex:
- {
- if (aCol > 1)
- {
- aColumnSizer = new TColumnSizer;
- aColumnSizer->IColumnSizer(fCalcDocument, aCol - 1, theMouse);
- this->PostCommand(aColumnSizer);
- }
- break;
- }
- } // switch!!
- fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
- fCalcDocument->fRowsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnIsSelected = FALSE;
- fCalcDocument->fEditCell = NULL;
- }
- } // TCellsView::DoMouseCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::DoSetupMenus() // override
- {
- TypeOfSelection selection;
-
- Inherited::DoSetupMenus();
-
- // If user isn't editing, then assume edit commands refer to cells
- if (!fCalcDocument->fEntryView->fTEditing)
- {
- SetEditCommandName(cCut, cCutCells);
- SetEditCommandName(cCopy, cCopyCells);
- SetEditCommandName(cClear, cClearCells);
-
- gClipboardMgr->CanPaste(kCalcScrapType);
- }
-
- selection = fCalcDocument->fSelectionType;
- Enable(cCut, selection != kNoSelection);
- Enable(cCopy, selection != kNoSelection);
- Enable(cClear, selection != kNoSelection);
- Enable(cSelectAll, TRUE);
- } // TCellsView::DoSetupMenus
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCellsView::DoMenuCommand(CommandNumber aCommandNumber) // override
- {
- TCellEditCommand * aCellEditCommand;
- TCellPasteCommand * aCellPasteCommand;
- RgnHandle cellsToSelect;
- CRect aRect;
-
-
- switch (aCommandNumber)
- {
- case cSelectAll:
- {
- aRect = fCalcDocument->fInUseBounds;
- aRect.right++;
- aRect.bottom++;
- cellsToSelect = MakeNewRgn();
- RectRgn(cellsToSelect, aRect);
- this->SetSelection(cellsToSelect, kDontExtend, kHighlight, kSelect);
- DisposeRgn(cellsToSelect);
- fCalcDocument->fSelectionType = kAllSelection;
- fCalcDocument->fRowsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnIsSelected = FALSE;
- break;
- }
-
- case cCut:
- case cCopy:
- case cClear:
- // If user isn't editing this cell, he must want the cell itself.
- {
- if (!fCalcDocument->fEntryView->fTEditing)
- {
- aCellEditCommand = new TCellEditCommand;
- aCellEditCommand->ICellEditCommand(fCalcDocument, aCommandNumber);
- this->PostCommand(aCellEditCommand);
- }
- else
- { // get ready for TextEdit operation
- fCalcDocument->fEditCell = fCalcDocument->GetCell(fCalcDocument->fEditRow, fCalcDocument->fEditColumn);
- Inherited::DoMenuCommand(aCommandNumber);
- }
- break;
- }
-
- case cPaste:
- {
- if (gClipboardMgr->fClipView->ContainsClipType(kCalcScrapType))
- {
- aCellPasteCommand = new TCellPasteCommand;
- aCellPasteCommand->ICellPasteCommand(fCalcDocument);
- this->PostCommand(aCellPasteCommand);
- }
- else
- { // paste text into entry view
- // was "WITH fCalcDocument"
- fCalcDocument->fEditCell = fCalcDocument->GetCell(fCalcDocument->fEditRow, fCalcDocument->fEditColumn);
- if (!fCalcDocument->fEntryView->fTEditing)
- fCalcDocument->fEntryView->SetEditMode();// prepare view for paste of text
-
- Inherited::DoMenuCommand(aCommandNumber);
- }
- break;
- }
-
- default:
- Inherited::DoMenuCommand(aCommandNumber);
- } // switch!!
- } // TCellsView::DoMenuCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoKeyEvent: This view only handles arrow keys, tab, return and enter. It
- // assumes the other keys are handled by the entry view object.
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::DoKeyEvent(TToolboxEvent* event)
- {
- ColumnNumber c = fCalcDocument->fEditColumn;
- RowNumber r = fCalcDocument->fEditRow;
-
- Boolean handled = TRUE;
-
- if (event->fText == chEnter)
- fCalcDocument->SetChangeCount(Max(fCalcDocument->fChangeCount + 1, 1));// enable Save - document may have changed Stay on same cell
- else if (event->fText == chTab || event->fText == chRight)
- c = Min(c + 1, fCalcDocument->fNoOfColumns);
- else if (event->fText == chLeft)
- c = Max(c - 1, (short)1);
- else if (event->fText == chUp)
- r = Max(r - 1, (short)1);
- else if (event->fText == chReturn || event->fText == chDown)
- {
- if (r) // only do this if a cell is selected
- r = Min(r + 1, fCalcDocument->fNoOfRows);
- }
- else
- handled = FALSE;
-
- if (handled)
- {
- GridCell aCell(c, r);
-
- // used to be one HUGE "with fCalcDocument"
- //-- why is the view messing so much with the doc's internals?????
-
- fCalcDocument->fEntryView->EditMode(FALSE);
- fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
- fCalcDocument->fRowsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnIsSelected = FALSE;
-
- SelectCell(aCell, kDontExtend, kHighlight, kSelect);
- if (fCalcDocument->fEntryView->fTouched)
- fCalcDocument->DoRecalculate(!kForceAutomatic, kUpdateDependencies);
- ScrollSelectionIntoView(TRUE);
-
- // fix the columns view
- VPoint minToSee(fCalcDocument->fColumnsView->GetColWidth(aCell.h), kCellHeight);
- aCell.v = 1;
-
- fCalcDocument->fColumnsView->RevealRect(fCalcDocument->fColumnsView->CellToVRect(aCell), minToSee, TRUE);
-
- // fix the rows view
- minToSee.h = kCellWidth;
- minToSee.v = kCellHeight;
- aCell.v = r;
- aCell.h = 1;
- fCalcDocument->fRowsView->RevealRect(fCalcDocument->fRowsView->CellToVRect(aCell), minToSee, TRUE);
-
- // redraw anything that has changed, so the user can see what's being scrolled to
- Update();
- }
- else
- {
- Inherited::DoKeyEvent(event);
- fCalcDocument->fEditCell = fCalcDocument->GetCell(r, c);
- }
- } // TCellsView::DoKeyEvent
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::AdornCol:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::AdornCol(short /*aCol*/, const VRect& area) // override
- {
- PenPat(&gColumnSeparatorPattern);
- PenSize(1, 1);
-
- CRect qdArea(ViewToQDRect(area));
- MoveTo(qdArea.right - 1, qdArea.top);
- LineToPt(qdArea[botRight] - CPoint(1, 1));
- } // TCellsView::AdornCol
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::AdornRow:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::AdornRow(short /*aCol*/, const VRect& area) // override
- {
- PenPat(&gRowSeparatorPattern);
- PenSize(1, 1);
-
- CRect qdArea(ViewToQDRect(area));
- MoveTo(qdArea.left, qdArea.bottom - 1);
- LineToPt(qdArea[botRight] - CPoint(1, 1));
- } // TCellsView::AdornRow
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DesignatorAsTEXT:
- //----------------------------------------------------------------------------------------
- #pragma segment GNonRes2
-
- Handle TCellsView::DesignatorAsTEXT(TDesignator* aDesignator)
- {
- CRect bounds;
- Handle textHandle;
- THandleStream * aHandleStream;
- CStr255 theString;
- RgnHandle aRgn;
-
- textHandle = NewPermHandle(100);
- aHandleStream = new THandleStream;
- aHandleStream->IHandleStream(textHandle, 10);
- aRgn = ((TRegionDesignator *)(aDesignator))->fDesignation;
- bounds = (*aRgn)->rgnBBox;
- bounds.bottom--;
- bounds.right--;
- CCellIterator iter(this, bounds[topLeft], bounds[botRight], kIterateForward, kIterateForward, CCellIterator::kIterateRowMajor);
- for (GridCell aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- {
- SignedByte aChar;
-
-
- // write out the cell's contents if the cell is in the designation && it exists
- if (PtInRgn(aCell, aRgn) && fCalcDocument->CellExists(aCell.v, aCell.h))
- {
- fCalcDocument->GetCell(aCell.v, aCell.h)->GetValueAsString(theString);
- aHandleStream->WriteBytes(&theString[1], theString.Length());// we can't use WriteString
- // because it writes the
- // length byte as well
- }
-
- // write out the delimitter - tab or cr
- if (aCell.h == bounds.right)
- aChar = 0x0D;
- else
- aChar = 0x09;
- aHandleStream->WriteBytes(&aChar, sizeof(SignedByte));
- }
- aHandleStream->Free();
- return textHandle;
- } // TCellsView::DesignatorAsTEXT
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoIdle:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- Boolean TCellsView::DoIdle(IdlePhase /* phase */)
- {
- KeyMap aKeyMap;
-
- GetKeys(aKeyMap);
- if (fLastOptionKey != IsOptionKeyDown())
- {
- fLastOptionKey = IsOptionKeyDown();
- gApplication->InvalidateCursorRgn(); // Since we track the option key, we now need
- // to recalculate cursor rgn and SET the
- // cursor at next opportunity
- }
- return FALSE; // Didn't free myself
- } // TCellsView::DoIdle
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoSetCursor:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::DoSetCursor(const VPoint& globalMouse,
- RgnHandle cursorRegion) // override
- {
- KeyMap aKeyMap;
- GetKeys(aKeyMap);
-
- // We may have been called from a mouse-moved. Be kind to the DoIdle
- fLastOptionKey = IsOptionKeyDown();
-
- if (fLastOptionKey)
- {
- SetCursor(*GetCursor(kGrabberHand));
- RectRgn(cursorRegion, &this->GetQDExtent());
- }
- else
- {
- VRect cellsExtent;
- short aRow = -1; // This value should never be used
- short aCol = -1; // This value should never be used
-
-
- GridCell aCell(-1, -1); // This value should never be used
- GridViewPart aGridViewPart = this->IdentifyPoint(globalMouse, aCell);
- aCol = aCell.h;
- aRow = aCell.v;
-
- switch (aGridViewPart) //since almost ß
- {
- case badChoice:
- break;
-
- case inColumn:
- case inVertex:
- {
- if (aCol > 1)
- {
- SetCursor(*GetCursor(kColumnSizingCursor));
- this->ColToVRect(Min(aCol, fNumOfCols), 1, cellsExtent);
- CRect columnQDExtent(this->ViewToQDRect(cellsExtent));
-
- // Which edge is the mouse closer to?
- if (labs(columnQDExtent.right - globalMouse.h) < labs(columnQDExtent.left - globalMouse.h))
- columnQDExtent.left = columnQDExtent.right;
-
- columnQDExtent.left = columnQDExtent.left - fColInset / 2;
- columnQDExtent.right = columnQDExtent.left + fColInset;
- RectRgn(cursorRegion, columnQDExtent);
- }
- break;
- }
- case inRow:
- case inCell:
- {
- SetCursor(*GetCursor(plusCursor));
- this->ColToVRect(Min(aCol, fNumOfCols), 1, cellsExtent);
- cellsExtent.Inset(VPoint(fColInset / 2, 0));// Account for the column resizer
- RectRgn(cursorRegion, &this->ViewToQDRect(cellsExtent));
- break;
- }
- } // switch
- }
- } // TCellsView::DoSetCursor
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DrawCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCellsView::DrawCell(GridCell aCell, const VRect& aRect) // override
- {
- TCell * theCell = fCalcDocument->GetExistingCell(aCell.v, aCell.h);
- if ((theCell != NULL) &&!theCell->fDeleted)
- {
- CStr255 theString;
- theCell->GetValueAsString(theString);
- SmartDrawString(theString, this->ViewToQDRect(aRect), fCalcDocument->GetColumn(aCell.h)->fFormat.fJustification);
- }
- } // TCellsView::DrawCell
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::GetVisibleCells:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- CRect TCellsView::GetVisibleCells()
- {
- this->Focus();
- VRect visibleVRect(GetVisibleRect());
- visibleVRect[topLeft] = GetScroller(TRUE)->fTranslation;
-
- return CRect(VPointToCell(visibleVRect[topLeft]), VPointToCell(visibleVRect[botRight]));
- } // TCellsView::GetVisibleCells
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::IsCellVisible:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- Boolean TCellsView::IsCellVisible(GridCell aCell)
- {
- return GetVisibleCells().Contains(aCell); // ??? use fCalcDocument.CellInRange
- } // TCellsView::IsCellVisible
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::PositionAtCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::PositionAtCell(GridCell aCell)
- {
- // position the cells view first
- VRect aRect(CellToVRect(aCell));
- VPoint minToSee(aRect.right - aRect.left, aRect.bottom - aRect.top);
- RevealRect(aRect, minToSee, TRUE);
-
- // fix up the columns view
- short r = aCell.v;
- minToSee.h = fCalcDocument->fColumnsView->GetColWidth(aCell.h);
- minToSee.v = kCellHeight;
- aCell.v = 1;
- fCalcDocument->fColumnsView->CellToVRect(aCell, aRect);
- fCalcDocument->fColumnsView->RevealRect(aRect, minToSee, TRUE);
-
- // fix up the rows view
- minToSee.h = kCellWidth;
- minToSee.v = kCellHeight;
- aCell.v = r;
- aCell.h = 1;
- fCalcDocument->fRowsView->CellToVRect(aCell, aRect);
- fCalcDocument->fRowsView->RevealRect(aRect, minToSee, TRUE);
- } // TCellsView::PositionAtCell
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ReSelect:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::ReSelect(RgnHandle cellRegion)
- {
- GridCell aCell;
- CRect rgnBBox = (*cellRegion)->rgnBBox;
-
- aCell = rgnBBox[topLeft];
- if (!this->IsCellVisible(aCell))
- this->PositionAtCell(aCell); // position cellRegion at top left of grid
-
- if (!EqualRect(rgnBBox, &((*fSelections)->rgnBBox)))
- {
- fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
- fCalcDocument->fRowsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnIsSelected = FALSE;
- this->SetSelection(cellRegion, kDontExtend, kHighlight, kSelect);
- }
- } // TCellsView::ReSelect
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ReSelectCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::ReSelectCell(GridCell aCell)
- {
- CRect cellRect;
-
- if (!this->IsCellVisible(aCell))
- this->PositionAtCell(aCell); // position aCell at top left of grid
-
- SetRect(cellRect, aCell.h, aCell.v, aCell.h + 1, aCell.v + 1);
- if (!EqualRect(cellRect, &((*fSelections)->rgnBBox)))
- {
- fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
- fCalcDocument->fRowsView->SetEmptySelection(kHighlight);
- fCalcDocument->fColumnIsSelected = FALSE;
- this->SelectCell(aCell, kDontExtend, kHighlight, kSelect);
- }
- } // TCellsView::ReSelectCell
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ScrollSelectionIntoView:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::ScrollSelectionIntoView(Boolean redraw) // override
- {
- if (!(EmptyRgn(fSelections)))
- { VRect topLeftRect(CellToVRect(((CRect &)(*fSelections)->rgnBBox)[topLeft]));
- RevealRect(topLeftRect, topLeftRect.GetSize(), redraw);
- }
- } // TCellsView::ScrollSelectionIntoView
-
- //----------------------------------------------------------------------------------------
- // TCellsView::SetCell:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::SetCell(GridCell aCell)
- {
- fCalcDocument->EditCell(); // cell formula = CString in entry view
- this->InvalidateCell(aCell); // redraw the cell
- } // TCellsView::SetCell
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::SetColWidth:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::SetColWidth(short aCol,
- short numOfCols,
- short aWidth) // override
- {
- Inherited::SetColWidth(aCol, numOfCols, aWidth);
-
- {
- // notify the cells that the width was changed
- CExistingCalcCellIterator iter(fCalcDocument);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- if (aCell->fColumn == aCol)
- aCell->Changed(mWidthChanged, this);
- }
- // recompute the border rgns
- {
- CAdornerIterator iter(this);
-
- for (TAdorner * anAdorner = iter.FirstAdorner(); iter.More(); anAdorner = iter.NextAdorner())
- {
- TSectionAdorner * aSectionAdorner;
-
- if ((anAdorner != NULL) && anAdorner->DescendsFrom(TSectionAdorner::GetClassDescStatic()))
- {
- aSectionAdorner = (TSectionAdorner *)(anAdorner);
- this->ComputeBorderRgn(aSectionAdorner->fBorderRegion, ((TRegionDesignator *)(aSectionAdorner->fSection)->fDesignator)->fDesignation);
- }
- }
- }
- } // TCellsView::SetColWidth
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::SetSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCellsView::SetSelection(RgnHandle cellsToSelect,
- Boolean extendSelection,
- Boolean highlight,
- Boolean select) // Override
- {
- CRect aQDRect;
-
- Inherited::SetSelection(cellsToSelect, extendSelection, highlight, select);
-
- if (fCalcDocument->fEntryView->fTouched) // "commit" last cell
- {
- fCalcDocument->EditCell(); /* change fEditCell's formula to the CString
- in fEntryView */
- fCalcDocument->fEntryView->SetToString(gEmptyString);
- }
-
- CRect boundRect;
- CPoint onePt(1,1);
-
- boundRect = (*cellsToSelect)->rgnBBox;
- if (!extendSelection && (boundRect.GetSize() == onePt))
- {
- CPoint cellTopLeft = ((CRect &)(*cellsToSelect)->rgnBBox)[topLeft];
-
- fCalcDocument->fEditColumn = cellTopLeft.h;
- fCalcDocument->fEditRow = cellTopLeft.v;
- fCalcDocument->fEditCell = fCalcDocument->fCells->GetCell(fCalcDocument->fEditRow, fCalcDocument->fEditColumn);
-
- fCalcDocument->fCoordView->ForceRedraw();
- fCalcDocument->fEntryView->SetEnable(TRUE); // Allow editing.
- }
- else
- if (!extendSelection)
- {
- if (boundRect.GetLength(hSel) == 1)
- fCalcDocument->fEditColumn = boundRect.left;
- else
- fCalcDocument->fEditColumn = 0;
-
- if (boundRect.GetLength(vSel) == 1)
- fCalcDocument->fEditRow = boundRect.top;
- else
- fCalcDocument->fEditRow = 0;
-
- fCalcDocument->fEditCell = NULL;
-
- fCalcDocument->fCoordView->ForceRedraw();
- fCalcDocument->fEntryView->SetEnable(FALSE); // Don't allow editing of thin air.
- }
-
- fCalcDocument->SetEntry(fCalcDocument->fEditRow, fCalcDocument->fEditColumn);// set entry view contents to new cell
- } // TCellsView::SetSelection
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoBreakFollowing:
- //----------------------------------------------------------------------------------------
- #pragma segment GNonRes2
-
- // Determines where page breaks occur for printing.
- VCoordinate TCellsView::DoBreakFollowing(VHSelect vhs,
- VCoordinate previousBreak,
- Boolean& /*Automatic*/) // override
- {
- VCoordinate thisBreak = 0;
- short rowsPerPage;
- short totalWidth;
- short width;
- short pageWidth;
- ColumnNumber firstCol;
- ColumnNumber c;
-
- VRect extentRect(GetExtent());
- switch (vhs)
- {
- case hSel:
- {
- rowsPerPage = (short)(GetPrintHandler()->GetViewPerPage().v / kCellHeight);
- thisBreak = previousBreak + (rowsPerPage * kCellHeight);
- break;
- }
- case vSel:
- {
- pageWidth = (short)GetPrintHandler()->GetViewPerPage().h;
- totalWidth = 0;
- // This section used to be "firstCol = ColumnAtCoord(previousBreak);"
- if (previousBreak == 0)
- firstCol = 1;
- else
- {
- c = 0;
- width = 0;
- do
- {
- ++c;
- width = width + GetColWidth(c);
- } while (width < previousBreak);
- firstCol = c + 1;
- }
- for (c = firstCol; c <= fCalcDocument->fNoOfColumns; c++)
- {
- width = this->GetColWidth(c);
- if (totalWidth + width <= pageWidth)
- totalWidth += width;
- else
- {
- thisBreak = previousBreak + totalWidth;
- break;
- }
- }
-
- // Did we set it?
- if (thisBreak == 0)
- thisBreak = previousBreak + totalWidth;
-
- if (thisBreak == previousBreak) // Prevent ∞ loop resizing a far-right column (L.T.!)
- thisBreak = extentRect.right;
- break;
- }
- }
- thisBreak = Min(thisBreak, extentRect[botRight][gOrthogonal[vhs]]);
-
- #if qDebug
- if ((thisBreak <= previousBreak) || gDebugPrinting)
- {
- fprintf(stderr, "TCellsView.DoBreakFollowing: previousBreak=");
- if (vhs == vSel)
- fprintf(stderr, "[v]%d thisBreak=[v]%d\n", previousBreak, thisBreak);
- else
- fprintf(stderr, "[h]%d thisBreak=[h]%d\n", previousBreak, thisBreak);
-
- if (thisBreak <= previousBreak)
- ProgramBreak("thisBreak <= previousBreak");
- }
- #endif
-
- return thisBreak;
- } // TCellsView::DoBreakFollowing
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::DoDrawPageBreak:
- //----------------------------------------------------------------------------------------
- #if qDebug
- #pragma segment GNonRes2
-
- void TCellsView::DoDrawPageBreak(VHSelect vhs,
- short /*whichBreak*/,
- VCoordinate loc,
- Boolean /*Automatic*/) // override
- {
- VPoint vPt;
- CPoint qdStartPt;
- CPoint qdEndPt;
-
- if (gDebugPrinting)
- {
- vPt[gOrthogonal[vhs]] = loc;
- vPt[vhs] = 0;
- qdStartPt = ViewToQDPt(vPt);
- vPt[vhs] = fSize[vhs] - ((CPoint &)gBreaksPenState.pnSize)[vhs];
- qdEndPt = ViewToQDPt(vPt);
-
- MoveToPt(qdStartPt);
- LineToPt(qdEndPt);
- }
- } // TCellsView::DoDrawPageBreak
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // TCellsView::GetPrintExtent: Overridden to provide for Print Selection command.
- //----------------------------------------------------------------------------------------
- #pragma segment GNonRes2
-
- VRect TCellsView::GetPrintExtent() // override
- {
- VRect printExtent;
- GridCell tlCell;
- GridCell brCell;
-
- if (((TCalcPrintHandler *)(GetPrintHandler()))->fCmdNumber == cPrintSelection)
- {
- tlCell = ((CRect &)(*fSelections)->rgnBBox)[topLeft];
- brCell = ((CRect &)(*fSelections)->rgnBBox)[botRight];
- brCell.h = Min(brCell.h - 1, fCalcDocument->fInUseBounds.right);
- brCell.v = Min(brCell.v - 1, fCalcDocument->fInUseBounds.bottom);
- }
- else
- {
- tlCell = fCalcDocument->fInUseBounds[topLeft];
- brCell = fCalcDocument->fInUseBounds[botRight];
- }
-
- VRect aRect(CellToVRect(tlCell));
- printExtent[topLeft] = aRect[topLeft];
- this->CellToVRect(brCell, aRect);
- printExtent[botRight] = aRect[botRight];
-
- #if qDebug
- if (gDebugPrinting)
- {
- fprintf(stderr, "printExtent %s\n", (const char*)printExtent);
- }
- #endif
-
- return printExtent;
- } // TCellsView::GetPrintExtent
-
- //----------------------------------------------------------------------------------------
- // TCellsView::UserSelectionChanged:
- //----------------------------------------------------------------------------------------
- #pragma segment MAViewRes
-
- void TCellsView::UserSelectionChanged(TView* changedView)
- {
- if (!fCalcDocument->fShowSectionBorders)
- {
- // invalidate all adorners
- CAdornerIterator iter(this);
-
- for (TAdorner* theAdorner = iter.FirstAdorner(); iter.More(); theAdorner = iter.NextAdorner())
- {
- if ((theAdorner->fIdentifier == kPublisherAdornerID)
- || (theAdorner->fIdentifier == kSubscriberAdornerID))
- {
- theAdorner->InvalidateAdorner(this);
- }
- }
- }
-
- Inherited::UserSelectionChanged(changedView);
- } // TCellsView::UserSelectionChanged
-
- //----------------------------------------------------------------------------------------
- // TCellsView::ContainsClipType:
- //----------------------------------------------------------------------------------------
- #pragma segment AClipBoard
-
- Boolean TCellsView::ContainsClipType(ResType aType)
- {
- return (aType == kCalcScrapType);
- } // TCellsView::ContainsClipType
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::WriteCalcScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment AClipBoard
-
- void TCellsView::WriteCalcScrap(Handle calcScrap)
- {
- long scrapOffset;
- ScrapInfoRecord scrapInfo;
- short cellsWritten;
- short i;
- CRect r;
-
- SetHandleSize(calcScrap, 0);
- scrapOffset = 0;
-
- scrapInfo.selection.top = 1;
- scrapInfo.selection.left = 1;
- scrapInfo.selection.bottom = fCalcDocument->fNoOfRows;
- scrapInfo.selection.right = fCalcDocument->fNoOfColumns;
- scrapInfo.noOfCells = fCalcDocument->fAllocatedCells;
- WriteScrap(calcScrap, scrapOffset, (unsigned char*) & scrapInfo, sizeof(scrapInfo));
-
- cellsWritten = 0;
-
- for (i = 1; i <= fCalcDocument->fNoOfRows; i++)
- {
- fCalcDocument->GetRow(i)->WriteToScrap(calcScrap, scrapOffset);
- }
-
- for (i = 1; i <= fCalcDocument->fNoOfColumns; i++)
- {
- fCalcDocument->GetColumn(i)->WriteToScrap(calcScrap, scrapOffset);
- }
-
- CExistingCalcCellIterator iter(fCalcDocument);
- for (TCell * aCell = iter.FirstCell(); iter.More(); aCell = iter.NextCell())
- {
- aCell->WriteToScrap(calcScrap, scrapOffset);
- cellsWritten = cellsWritten + 1;
- } // EachExistingCellDo(WriteCellToScrap);
-
- #if qDebug
- fprintf(stderr, "WriteCalcScrap: Number of cells written:%d\n", cellsWritten);
- if (cellsWritten != scrapInfo.noOfCells)
- {
- fprintf(stderr, "WriteCalcScrap: Incorrect number of cells written.\n");
- fprintf(stderr, " Should be %d , was %d\n", scrapInfo.noOfCells, cellsWritten);
- ProgramBreak("");
- }
- #endif
-
- } // TCellsView::WriteCalcScrap
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::WriteTextScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment AClipBoard
-
- void TCellsView::WriteTextScrap(Handle textScrap)
- {
- RowNumber r;
- ColumnNumber c;
- CStr255 theText;
- CStr255 tabString = CStr255("\t");
- CStr255 returnString = CStr255("\n");
- long scrapOffset;
- GrafPtr savedPort;
-
- GetPort(&savedPort); // Use work port because GetValueAsString
- SetPort(gWorkPort); // …sets the current port's font
-
- SetHandleSize(textScrap, 0);
- scrapOffset = 0;
-
- for (r = 1; r <= fCalcDocument->fNoOfRows; r++)
- {
- for (c = 1; c <= fCalcDocument->fNoOfColumns; c++)
- {
- if (fCalcDocument->CellExists(r, c))
- fCalcDocument->GetCell(r, c)->GetValueAsString(theText);
- else
- theText.Empty();
- if (c > 1)
- theText = tabString + theText;
- WriteScrap(textScrap, scrapOffset, &(theText.fStr[1]), theText.Length());
- //WriteString(theText);
- }
- theText = returnString;
- WriteScrap(textScrap, scrapOffset, &(theText.fStr[1]), theText.Length());
- //WriteString(theText);
- }
-
- SetPort(savedPort);
- } // TCellsView::WriteTextScrap
-
-
- //----------------------------------------------------------------------------------------
- // TCellsView::WriteToDeskScrap:
- //----------------------------------------------------------------------------------------
- #pragma segment AClipBoard
-
- void TCellsView::WriteToDeskScrap() // override
- {
- Handle textScrap;
- Handle calcScrap;
- OSErr err;
-
- textScrap = NewPermHandle(0);
- this->WriteTextScrap(textScrap);
- err = gClipboardMgr->PutDeskScrapData(kTextScrapType, textScrap);
- textScrap = DisposeIfHandle(textScrap);
-
- FailOSErr(err);
-
- calcScrap = NewPermHandle(0);
- this->WriteCalcScrap(calcScrap);
- err = gClipboardMgr->PutDeskScrapData(kCalcScrapType, calcScrap);
- calcScrap = DisposeIfHandle(calcScrap);
-
- FailOSErr(err);
- } // TCellsView::WriteToDeskScrap
-
-
- //========================================================================================
- // CLASS TCalcPrintHandler
- //========================================================================================
- #undef Inherited
- #define Inherited TStdPrintHandler
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcPrintHandler, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcPrintHandler destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCalcPrintHandler::~TCalcPrintHandler()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCalcPrintHandler::CalcViewPerPage:
- //----------------------------------------------------------------------------------------
- #pragma segment PrintNonRes
-
- VPoint TCalcPrintHandler::CalcViewPerPage() // override
- {
- VPoint amtPerPage(Inherited::CalcViewPerPage());
-
- long noOfRows = amtPerPage.v / kCellHeight;
- amtPerPage.v = noOfRows * kCellHeight;
-
- return amtPerPage;
- } // TCalcPrintHandler::CalcViewPerPage
-
-
- //----------------------------------------------------------------------------------------
- // TCalcPrintHandler::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment PrintRes
-
- void TCalcPrintHandler::DoMenuCommand(CommandNumber aCommandNumber) // override
- {
- fCmdNumber = aCommandNumber; // Save command number for GetPrintExtent
- if (aCommandNumber == cPrintSelection)
- aCommandNumber = cPrint; // proceed like regular Print
- Inherited::DoMenuCommand(aCommandNumber);
- } // TCalcPrintHandler::DoMenuCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCalcPrintHandler::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment PrintRes
-
- void TCalcPrintHandler::DoSetupMenus() // override
- {
- Inherited::DoSetupMenus();
- Enable(cPrintSelection, gCouldPrint && (fView != NULL));
- } // TCalcPrintHandler::DoSetupMenus
-
-
- //========================================================================================
- // CLASS TEntryView
- //========================================================================================
- #undef Inherited
- #define Inherited TTEView
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TEntryView, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TEntryView destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TEntryView::~TEntryView()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoPostCreate:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TEntryView::DoPostCreate(TDocument* itsDocument)
- {
- fDocument = itsDocument;
- fCalcDocument = (TCalcDocument *)(itsDocument);
- fTouched = FALSE;
- fTEditing = FALSE;
- fFirstEdit = FALSE;
- fOldString.Empty();
-
- fControlChars = macroAsSetElem(chBackspace); // The others are handled by the cells view
- } // TEntryView::DoPostCreate
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoKeyEvent:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::DoKeyEvent(TToolboxEvent* event) // override
- {
- // If this is the first character, wipe out old value and activate caret.
- if ((!fTEditing) && ((event->fText >= chSpace) || (event->fText == chBackspace)))
- SetEditMode();
-
- Inherited::DoKeyEvent(event);
- } // TEntryView::DoKeyEvent
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoMakeTypingCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- TTETypingCommand* TEntryView::DoMakeTypingCommand(const CStr2& ch) // override
- {
- TCalcTypingCommand* aTypingCommand = new TCalcTypingCommand;
- aTypingCommand->ICalcTypingCommand(this, ch);
- return aTypingCommand;
- } // TEntryView::DoMakeTypingCommand
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoMouseCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TEntryView::DoMouseCommand(VPoint& theMouse,
- TToolboxEvent* event,
- CPoint hysteresis) // override
- {
- // If no characters typed, active caret, then handle mouse down.
- if (!fTouched)
- {
- this->SetActive(TRUE);
- fTouched = TRUE;
- }
- this->EditMode(TRUE);
- Inherited::DoMouseCommand(theMouse, event, hysteresis);
- } // TEntryView::DoMouseCommand
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoMenuCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment TESelCommand
-
- void TEntryView::DoMenuCommand(CommandNumber aCommandNumber)
- {
- if (fTEditing)
- {
- // TTEView.DoMenuCommand will process the Cut/ Paste/ Clear for us, but we only want
- // it to if we're editing in the first place. Otherwise, we can't call inherited,
- // because then CPC commands we recieve should go to the CellsView, so therefore
- // we just tell the next handler to do its thing, thus continuing the chain (see
- // TEventHandler::DoMenuCommand)
-
- Inherited::DoMenuCommand(aCommandNumber);
- }
- else
- {
- TEventHandler* nextHandler = this->GetNextHandler();
- if (nextHandler)
- nextHandler->HandleMenuCommand(aCommandNumber);
- }
- } // TEntryView::DoMenuCommand
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::DoSetupMenus:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::DoSetupMenus() // override
- {
- if (fTEditing)
- {
- // TTEView.DoSetupMenus will setup the Edit menu commands for us if we're editing.
- // But if we're not, we can't call inherited, because then they'll still get set
- // up. To give somebody else the chance to setup, we'll just move onto the next
- // handler (see TEventHandler::DoSetupMenus )
- Inherited::DoSetupMenus();
-
- SetEditCommandName(cCut, cCutText);
- SetEditCommandName(cCopy, cCopyText);
- SetEditCommandName(cClear, cClearText);
- }
- else
- {
- TEventHandler* nextHandler = this->GetNextHandler();
- if (nextHandler)
- nextHandler->HandleSetupMenus();
- }
- } // TEntryView::DoSetupMenus
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::SetupUndoMenu:
- //----------------------------------------------------------------------------------------
- // #pragma segment ARes
-
- // void TEntryView::SetupUndoMenu() // override
- // {
- // if (fTEditing)
- // {
- // // TTEView.SetupUndoMenu will handle setting the undo for us, but we only want it to
- // // if we're editing in the first place. Otherwise, we can't call inherited, because then
- // // CPC commands we recieve should go to the CellsView, so therefore we just tell the next
- // // handler to do its thing, thus continuing the chain (see TEventHandler::DoMenuCommand)
- //
- // Inherited::SetupUndoMenu();
- // }
- // } // TEntryView::SetupUndoMenu
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::Draw(const VRect& area) // override
- {
- Inherited::Draw(area);
-
- // We want a rectangle around the whole view
- PenSize(1, 1);
- PenPat(&qd.black);
- FrameRect(&GetQDExtent());
-
- Inherited::Draw(area);
- } // TEntryView::Draw
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::WantsToBeTarget:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- Boolean TEntryView::WantsToBeTarget() // override
- {
- // Return false for now, because Calc currently relies on the cells view being the target
- return FALSE;
- } // TEntryView::WantsToBeTarget
-
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::EditMode:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TEntryView::EditMode(Boolean editing)
- {
- fTEditing = editing;
- fFirstEdit = FALSE; // set to true only by DoKeyEvent
-
- if (editing)
- GetAsString(fOldString); // save previous CString for Undo/Redo
-
- // else
- // {
- // // disable undo/redo for TTECommands. ???
- // // There must be a better way! this is
- // // disgusting!
- // TCommand* lastCommand = this->GetLastCommand();
- // if (lastCommand != NULL)
- // if (lastCommand->GetClassDescDynamic()->GetBaseClass() == TTECommand::GetClassDescStatic())
- // lastCommand->fCanUndo = FALSE;
- //
- // }
- } // TEntryView::EditMode
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::GetAsString:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::GetAsString(CStr255& theString)
- {
- CharsHandle theText = (CharsHandle)(this->ExtractText());
- short numberOfChars = (short)Min(255, GetHandleSize(Handle(theText)));
- theString.CopyFrom((void*)*theText, Min(255, numberOfChars));
- } // TEntryView::GetAsString
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::SetActive:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::SetActive(Boolean state)// Override
- {
- Inherited::SetActive(state);
-
- if (Focus())
- {
- CRect r;
- this->GetQDExtent(r);
- InsetRect(r, 2, 2);
- this->InvalidateRect(r);
- }
- } // TEntryView::SetActive
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::SetEditMode:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TEntryView::SetEditMode()
- {
- this->EditMode(TRUE); // sets fFirstEdit to false
- fFirstEdit = TRUE; // the only place it is set to true
- SetToString(gEmptyString);
- this->SetActive(TRUE);
- fTouched = TRUE;
- } // TEntryView::SetEditMode
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::SetToString:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TEntryView::SetToString(const CStr255& theString)
- {
- CStr255 tempString = theString;
-
- this->Focus();
- SetJustification(teFlushDefault, kDontRedraw);// initialize text to system-justified
- this->SetActive(FALSE);
- fTouched = FALSE;
- TESetText(&((CStr255 &)tempString)[1], tempString.Length(), fHTE);
- // first cast away the const so that the [] operator returns a reference to
- // the actual location in the array (otherwise returns the character itself
- // on the stack) since we're interested in an address, not what it contains,
- // then actually do the [] operator to get the start of the CString to stuff.
- TESetSelect(0, 32768, fHTE); // hilight the entire CString.
- } // TEntryView::SetToString
-
-
- //----------------------------------------------------------------------------------------
- // TEntryView::SwapStrings:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TEntryView::SwapStrings()
- {
- CStr255 newString;
-
- newString = fOldString;
- GetAsString(fOldString);
- SetToString(newString);
- fTouched = TRUE;
- } // TEntryView::SwapStrings
-
-
- //========================================================================================
- // CLASS TCoordView
- //========================================================================================
- #undef Inherited
- #define Inherited TView
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCoordView, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCoordView destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCoordView::~TCoordView()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCoordView::DoPostCreate:
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TCoordView::DoPostCreate(TDocument* itsDocument)
- {
- fCalcDocument = (TCalcDocument *)(itsDocument);
- } // TCoordView::DoPostCreate
-
-
- //----------------------------------------------------------------------------------------
- // TCoordView::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCoordView::Draw(const VRect& area)
- {
- CStr255 aString;
- CStr255 anotherString;
- CRect aRect;
-
- if (fCalcDocument->fEditColumn > 0)
- fCalcDocument->fColumnsView->CoordToString(fCalcDocument->fEditColumn, aString);
- else
- aString = " ";
-
- if (fCalcDocument->fEditRow > 0)
- NumToString(fCalcDocument->fEditRow, anotherString);
- else
- anotherString = " ";
-
- aString = aString + anotherString;
- SetTheFont(kEntryFont, kEntryFontSize, bold);
- SetRect(aRect, 2, 0, 46, kEntryHeight);
- SmartDrawString(aString, aRect, teFlushDefault);
-
- Inherited::Draw(area);
- } // TCoordView::Draw
-
-
- //========================================================================================
- // CLASS TCalcSelectCommand
- //========================================================================================
- #undef Inherited
- #define Inherited TCellSelectCommand
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcSelectCommand, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcSelectCommand destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCalcSelectCommand::~TCalcSelectCommand()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCalcSelectCommand::ICalcSelectCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TCalcSelectCommand::ICalcSelectCommand(TCalcDocument* itsDocument,
- TGridView* itsView,
- VPoint& theMouse,
- Boolean theShiftKey,
- Boolean theCommandKey)
- {
- this->ICellSelectCommand(itsView, theMouse, theShiftKey, theCommandKey);
- fCalcDocument = itsDocument;
- itsDocument->fEntryView->EditMode(FALSE);
- } // TCalcSelectCommand::ICalcSelectCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCalcSelectCommand::ComputeNewSelection:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcSelectCommand::ComputeNewSelection(GridCell& clickedCell) // override
- {
- CRect r;
-
- if (fGridView->CanSelectCell(clickedCell))
- {
- Pt2Rect(fAnchorCell, clickedCell, r);
- ++r.right;
- ++r.bottom;
- RectRgn(fThisSelection, r);
-
- // add in the designated region for subscribers
- #if qDebug
- UseTemporaryRegion("TCalcSelectCommand.ComputeNewSelection");
- #endif
-
- CSectionIterator iter(fCalcDocument); // for each section do
- for (TSection * aSection = iter.FirstSection(); iter.More(); aSection = iter.NextSection())
- {
- if (!aSection->IsCanceled() && aSection->GetSectionType() == stSubscriber)
- {
- SectRgn(fThisSelection, ((TRegionDesignator *)(aSection->fDesignator))->fDesignation, gTemporaryRegion);
- if (!EmptyRgn(gTemporaryRegion))// if this section is in selection
- UnionRgn(fThisSelection, ((TRegionDesignator *)(aSection->fDesignator))->fDesignation, fThisSelection);
- }
- } // THIS WAS fCalcDocument->EachSectionDo(DoToSection);
- #if qDebug
- DoneWithTemporaryRegion();
- #endif
-
- if (fCommandKey && (!fGridView->fSingleSelection))
- if (fDeselecting)
- DiffRgn(fPreviousSelection, fThisSelection, fThisSelection);
- else
- UnionRgn(fPreviousSelection, fThisSelection, fThisSelection);
- }
- } // TCalcSelectCommand::ComputeNewSelection
-
-
-
- //========================================================================================
- // CLASS TCalcTypingCommand
- //========================================================================================
- #undef Inherited
- #define Inherited TTETypingCommand
-
- #pragma segment ClassDescRes
- MA_DEFINE_CLASS_M1(TCalcTypingCommand, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TCalcTypingCommand destructor
- //----------------------------------------------------------------------------------------
- #pragma segment MADestructorRes
-
- TCalcTypingCommand::~TCalcTypingCommand()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TCalcTypingCommand::ICalcTypingCommand:
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcTypingCommand::ICalcTypingCommand(TTEView* itsTEView,
- const CStr2& itsFirstChar) // override
- {
- Inherited::ITETypingCommand(itsTEView, itsFirstChar);
- fCellsView = ((TEntryView *)(itsTEView))->fCalcDocument->fCellsView;
- fTargetCell = fCellsView->FirstSelectedCell();
- } // TCalcTypingCommand::ICalcTypingCommand
-
-
- //----------------------------------------------------------------------------------------
- // TCalcTypingCommand::AddCharacter: Switch the entry view from left to right
- // justification when the typed characters overflow the entry box, and switch back to left
- // justification if the text shrinks enough
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TCalcTypingCommand::AddCharacter(const CStr2& ch)// override
- {
- if ((ch != chBackspace) && (fTEView->fJustification == teFlushDefault))
- {
- short selRight = (*fHTE)->selRect.right;
- if ((selRight < 0) || (selRight > (*fHTE)->destRect.right))
- selRight = (*fHTE)->selPoint.h;
- if ((selRight + StringWidth(ch) > (*fHTE)->destRect.right))
- fTEView->SetJustification(teFlushRight, kDontRedraw);
- }
-
- Inherited::AddCharacter(ch);
-
- if ((ch == chBackspace) && (fTEView->fJustification == teFlushRight))
- {
- CStr255 aString;
-
- GetDialogItemText((*fHTE)->hText, aString);
- if (StringWidth(aString) < LengthRect((*fHTE)->destRect, hSel))
- fTEView->SetJustification(teFlushDefault, kRedraw);
- }
- } // TCalcTypingCommand::AddCharacter
-
-
- //----------------------------------------------------------------------------------------
- // TCalcTypingCommand::UndoIt: If the selection has changed since this command was
- // created, restore it to the target cell before Undoing.
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcTypingCommand::UndoIt() // override
- {
- fCellsView->ReSelectCell(fTargetCell); // make sure fTargetCell is selected
- TEntryView * entryView = (TEntryView *)(fTEView);
- if ((entryView->fTEditing) && (!entryView->fFirstEdit))
- Inherited::UndoIt(); // TTE undo
- else
- {
- entryView->SwapStrings(); // exchange current and saved strings
- fCellsView->SetCell(fTargetCell); // change fTargetCell's contents and redraw it
- }
- } // TCalcTypingCommand::UndoIt
-
-
- //----------------------------------------------------------------------------------------
- // TCalcTypingCommand::RedoIt: If the selection has changed since this command was
- // created, restore it to the target cell before Redoing.
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TCalcTypingCommand::RedoIt() // override
- {
- fCellsView->ReSelectCell(fTargetCell); // make sure fTargetCell is selected
- TEntryView * entryView = (TEntryView *)(fTEView);
- if ((entryView->fTEditing) && (!entryView->fFirstEdit))
- Inherited::RedoIt(); // TTE redo
- else
- {
- entryView->SwapStrings(); // exchange current and saved strings
- fCellsView->SetCell(fTargetCell); // change fTargetCell's contents and redraw it
- }
- } // TCalcTypingCommand::RedoIt
-
- //----------------------------------------------------------------------------------------
- // End of UCalc.cp
-
- #pragma segment Inline
-